<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="工序编号" prop="procedureNo">
        <el-input
          v-model="queryParams.procedureNo"
          placeholder="请输入工序编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="" prop="procedureName">
        <el-input v-model="form.procedureName" placeholder="请输入工序名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="工序分类编号" prop="procedureClassifyNo">
        <el-input
          v-model="queryParams.procedureClassifyNo"
          placeholder="请输入工序分类编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建角色" prop="createRole">
        <el-input
          v-model="queryParams.createRole"
          placeholder="请输入创建角色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['base:procedure:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:procedure:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:procedure:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['base:procedure:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="procedureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="工序编号" align="center" prop="procedureNo" />
      <el-table-column label="工序名称" align="center" prop="procedureName" />
      <el-table-column label="工序分类" align="center" prop="procedureClassifyName" />
      <el-table-column label="班组" align="center" prop="teamName" />
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="创建角色" align="center" prop="createRole" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="comment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['base:procedure:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['base:procedure:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改工序对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="queryParams" :rules="rules" label-width="80px">
        <!-- <el-form-item label="工序编号" prop="procedureNo">
          <el-input v-model="form.procedureNo" placeholder="请输入工序编号" />
        </el-form-item> -->
        <el-form-item label="工序名称" prop="procedureName">
          <el-input v-model="queryParams.procedureName" placeholder="请输入工序名称" />
        </el-form-item>
        <!-- <el-form-item label="工序分类" prop="procedureClassifyNo">
          <el-input v-model="form.procedureClassifyNo" placeholder="请输入工序分类编号" />
        </el-form-item> -->
        <el-form-item label="工序分类" prop="procedureClassifyNo">
          <el-select v-model="queryParams.procedureClassifyNo" @click="selectsGx"
            placeholder="请选择文章标签">
            <el-option v-for="item in  gxList" :key="item.val" :label="item.procedureClassifyName"  :value="item.procedureClassifyNo">
            </el-option>
          </el-select>


        </el-form-item>


        <el-form-item label="添加班组" prop="banzu">
          <el-select v-model="value" multiple filterable allow-create default-first-option @click="selects"
            placeholder="请选择班组">
            <el-option v-for="item in  list" :key="item.value" :label="item.teamName"  :value="item.teamNo">
            </el-option>
          </el-select>


        </el-form-item>


        <!-- <el-form-item label="创建角色" prop="createRole">
          <el-input v-model="form.createRole" placeholder="请输入创建角色" />
        </el-form-item> -->
        <el-form-item label="备注" prop="comment">
          <el-input v-model="queryParams.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcedure, getProcedure, delProcedure, addProcedure, updateProcedure, listManagement ,listProcedureClassify} from "@/api/base/procedure";
import LogoVue from "../../../layout/components/Sidebar/Logo.vue";

export default {
  name: "Procedure",
  data() {
    return {
      gxList:[],
      list: [],
      value: [],
      val:[],      
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序表格数据
      procedureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        procedureNo: null,
        procedureName: null,
        procedureClassifyNo: null,
        createUser: null,
        createRole: null,
        createTime: null,
        procedureClassifyName: null,
        comment: null,
        teamName: null,
        teamNoList:[]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        procedureName: [
          { required: true, message: "工序编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 弹框工序分类
    selectsGx(){
        listProcedureClassify(this.queryParams).then(ok=>{
console.log(ok.rows)
this.gxList = ok.rows
});
    },
    selects() {
      listManagement(this.queryParams).then(response => {
        this.list = response.rows;
      });
    },

    /** 查询工序列表 */
    getList() {
      this.loading = true;
      listProcedure(this.queryParams).then(response => {
        this.procedureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        procedureNo: null,
        procedureName: null,
        procedureClassifyNo: null,
        createUser: null,
        createRole: null,
        createTime: null,
        comment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序";
      this.selects();
      this.selectsGx();

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProcedure(id).then(response => {
        this.queryParams = response.data;
        this.open = true;
        this.title = "修改工序";
      });
      /////////////////////////
      this.selects();
      this.selectsGx();

    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
      
       
        if (valid) {
          if (this.queryParams.id != null) {
            updateProcedure(this.queryParams).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.queryParams.teamNoList=[...this.value];
            console.log("this.queryParams",this.queryParams);
            addProcedure(this.queryParams).then(response => {
              console.log(response,11111111111111111)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              
            });
          }
        }
      });
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工序编号为"' + ids + '"的数据项？').then(function () {
        return delProcedure(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/procedure/export', {
        ...this.queryParams
      }, `procedure_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
